import React, { useEffect } from 'react';
import {
  Form,
  Modal,
  Grid,
  Input,
  InputNumber,
  TimePicker,
  Checkbox,
} from '@arco-design/web-react';
import * as Model from '@/services/model';
import { ActionTypes } from './constants';
const { useForm } = Form;
const { Row, Col } = Grid;
interface DetailModalProps {
  permissionInfo?: Model.Role.Permission | false;
  onCancel: () => void;
  onOk: (params: Model.Truck) => void;
}

export const DetailModal = (props: DetailModalProps) => {
  const [form] = useForm();

  useEffect(() => {
    if (props.permissionInfo !== false) {
      console.log(props.permissionInfo);
      form.setFieldsValue(props.permissionInfo);
    }
  }, [props.permissionInfo]);

  const submit = async () => {
    const res = await form.validate();
    console.log(res);
    if (res) {
      props.onOk(res);
    }
  };
  return (
    <Modal
      visible={props.permissionInfo !== false}
      title={`${
        props.permissionInfo && props.permissionInfo.id ? '修改' : '新增'
      }权限`}
      mountOnEnter
      unmountOnExit
      onCancel={props.onCancel}
      onOk={submit}
      alignCenter={false}
      okText="提交"
      cancelText="取消"
      style={{ width: 600 }}
    >
      <Form
        form={form}
        labelAlign="left"
        labelCol={{ span: 6 }}
        wrapperCol={{ span: 18 }}
      >
        <Row gutter={24}>
          <Col span={24}>
            <Form.Item label="权限code" field="resource" required>
              <Input placeholder="请输入权限code" allowClear />
            </Form.Item>
          </Col>
          <Col span={24}>
            <Form.Item label="权限名称" field="name" required>
              <Input placeholder="请输入权限名称" allowClear />
            </Form.Item>
          </Col>

          <Col span={24}>
            <Form.Item label="权限操作" field="actions" required>
              <Checkbox.Group>
                {Object.keys(ActionTypes).map((item, index) => (
                  <Checkbox key={item} value={item}>
                    {ActionTypes[item]}
                  </Checkbox>
                ))}
              </Checkbox.Group>
            </Form.Item>
          </Col>
        </Row>
      </Form>
    </Modal>
  );
};
